package com.lucene.blog.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

/**
 * @Description 日期工具类
 * @author yachao
 *
 */
public class DateUtil {
	
	/**
	 * 日期对象转字符串
	 * @param data
	 * @param format
	 * @return
	 */
	public static String dateToString(Date date, String format) {
		String result = "";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		
		if(date != null) {
			result = sdf.format(date);
		}
		return result;
	}
	
	/**
	 * 字符串转日期对象
	 * @param str
	 * @param format
	 * @return
	 * @throws Exception
	 */
	public static Date changeStringToDate(String str, String format) throws Exception {
		if(StringUtils.isEmpty(str)) {
			return null;
		}
		
		SimpleDateFormat sdf =  new SimpleDateFormat(format);
		return sdf.parse(str);
	}
	
	public static String getCurrentDateStr() throws Exception {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
		return sdf.format(date);
	}
}
